﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using SilverlightApplication1;
using SilverlightApplication1.DataTypes;

namespace SilverlightApplication1
{
    public partial class AdvancedSearch : UserControl
    {
        public AdvancedSearch()
        {
            InitializeComponent();

            txtFilCategory.Tag = 0;
            txtSortFirst.Tag = 0;
            txtSortSecond.Tag = 0;
            txtSortThird.Tag = 0;
            txtSortSecond.IsTabStop = false;
            btnSortSecond.IsEnabled = false;
            txtSortThird.IsTabStop = false;
            btnSortThird.IsEnabled = false;
        }

        public void SetFocus()
        {
            txtFilAuthor.Focus();
        }

        #region ComboBox Category

        //txtFiltCategory
        private void txtFilCategory_GotFocus(object sender, RoutedEventArgs e)
        {
            txtFilCategory.Select(0, txtFilCategory.Text.Length);
        }
        private void txtFilCategory_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case System.Windows.Input.Key.Down:
                case System.Windows.Input.Key.Up:
                    if (lstItems.SelectedItem == null)
                    {
                        lstItems.SelectedIndex = 0;
                    }
                    btnFilCategory.Focus();
                    lstItems.Visibility = Visibility.Visible;
                    toVisibleCategory.Begin();
                    break;

                case System.Windows.Input.Key.Delete:
                    txtFilCategory.Text = "";
                    txtFilCategory.Tag = 0;
                    lstItems.SelectedIndex = -1;
                    break;

                case System.Windows.Input.Key.Enter:
                    txtFilMaxPrice.Focus();
                    break;
            }
        }

        //btnFiltCategory
        private void btnFilCategory_Click(object sender, RoutedEventArgs e)
        {
            if (lstItems.Visibility == Visibility.Collapsed)
            {
                if (lstItems.SelectedIndex == -1)
                {
                    lstItems.SelectedIndex = 0;
                }
                lstItems.Visibility = Visibility.Visible;
                toVisibleCategory.Begin();
            }
            else
            {
                txtFilCategory.Focus();
            }
        }

        //lstItems
        private void lstItems_LostFocus(object sender, RoutedEventArgs e)
        {
            if (!FocusManager.GetFocusedElement().GetType().ToString().Equals("System.Windows.Controls.ListBoxItem"))
            // !lstItems.Equals(((ListBoxItem)FocusManager.GetFocusedElement()).Parent))
            {
                lstItems.Visibility = Visibility.Collapsed;
            }
        }
        private void lstItems_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lstItems.SelectedItem != null && !lstItems.SelectedItem.Equals(sender))
            {
                txtFilCategory.IsTabStop = false;
                txtFilCategory.Text = ((TextBlock)((ListBoxItem)lstItems.SelectedItem).Content).Text;
                txtFilCategory.Tag = ((TextBlock)((ListBoxItem)lstItems.SelectedItem).Content).Tag;
                txtFilCategory.IsTabStop = true;
            }
        }
        private void toVisibleCategory_Completed(object sender, EventArgs e)
        {
            ((ListBoxItem)lstItems.Items[lstItems.SelectedIndex]).Focus();
        }

        //Item
        private void item_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Enter)
            {
                txtFilCategory.Focus();
            }
        }
        private void item_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            txtFilCategory.Focus();
        }

        #endregion


        #region ComboBox's Sort By

        //txtSortBy
        private void txtSortBy_GotFocus(object sender, RoutedEventArgs e)
        {
            ((TextBox)sender).Select(0, ((TextBox)sender).Text.Length);
        }
        private void txtSortBy_KeyDown(object sender, KeyEventArgs e)
        {
            ListBox lstAux = null;
            Button btnAux = null;
            Storyboard stbAux = null;

            switch (((TextBox)sender).Name)
            {
                case "txtSortFirst":
                    lstAux = lstSortFirst;
                    btnAux = btnSortFirst;
                    stbAux = toVisibleSortFirst;
                    break;
                case "txtSortSecond":
                    lstAux = lstSortSecond;
                    btnAux = btnSortSecond;
                    stbAux = toVisibleSortSecond;
                    break;
                case "txtSortThird":
                    lstAux = lstSortThird;
                    btnAux = btnSortThird;
                    stbAux = toVisibleSortThird;
                    break;
            }

            switch (e.Key)
            {
                case System.Windows.Input.Key.Down:
                case System.Windows.Input.Key.Up:
                    if (lstAux.SelectedItem == null)
                    {
                        if (lstAux.Name.Equals("lstSortFirst"))
                        {
                            lstAux.SelectedIndex = 0;
                        }
                        else if (lstAux.Name.Equals("lstSortSecond"))
                        {
                            //Si el Sort First tiene seleccionado el primer elemento, 
                            //entonces que éste seleccione el seg, en los otros 2 casos posibles
                            //éste selecciona el primero (Acordarse: Tag = Index del ListBox + 1)
                            if (((int)txtSortFirst.Tag) == 1)
                            {
                                lstAux.SelectedIndex = 1;
                            }
                            else
                            {
                                lstAux.SelectedIndex = 0;
                            }
                        }
                        else
                        {
                            if (!((((int)txtSortFirst.Tag) == 1) || (((int)txtSortSecond.Tag) == 1)))
                            {
                                lstAux.SelectedIndex = 0;
                            }
                            else if (!((((int)txtSortFirst.Tag) == 2) || (((int)txtSortSecond.Tag) == 2)))
                            {
                                lstAux.SelectedIndex = 1;
                            }
                            else
                            {
                                lstAux.SelectedIndex = 2;
                            }
                        }

                    }
                    btnAux.Focus();
                    lstAux.Visibility = Visibility.Visible;
                    stbAux.Begin();
                    break;

                case System.Windows.Input.Key.Delete:
                    ((TextBox)sender).Tag = 0;
                    ((TextBox)sender).Text = "";
                    lstAux.SelectedIndex = -1;
                    break;
                case System.Windows.Input.Key.Enter:
                    TextBox aux = ((TextBox)sender);
                    if (aux.Name == "txtSortFirst")
                    {
                        txtSortSecond.Focus();
                    }
                    if (aux.Name == "txtSortSecond")
                    {
                        txtSortThird.Focus();
                    }
                    else if (aux.Name == "txtSortThird")
                    {
                        txtFilAuthor.Focus();
                    }
                    break;
            }
        }

        private void txtSortByF_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (((int)txtSortFirst.Tag) > 0)
            {
                btnSortSecond.IsEnabled = true;
                txtSortSecond.IsTabStop = true;
                if (((int)txtSortFirst.Tag) == ((int)txtSortSecond.Tag))
                {
                    txtSortSecond.Tag = 0;
                    txtSortSecond.Text = "";
                    lstSortSecond.SelectedIndex = -1;
                }

            }
            else
            {
                btnSortSecond.IsEnabled = false;
                txtSortSecond.IsTabStop = false;
                txtSortSecond.Tag = 0;
                txtSortSecond.Text = "";
                lstSortSecond.SelectedIndex = -1;
            }

            if ((int)txtSortFirst.Tag == 1)
            {
                rbtnPOrderFAsc.Visibility = Visibility.Visible;
                //default
                rbtnPOrderFAsc.IsChecked = true;
                rbtnPOrderFDesc.Visibility = Visibility.Visible;
            }
            else if (rbtnPOrderFAsc.Visibility == Visibility.Visible)
            {
                rbtnPOrderFAsc.Visibility = Visibility.Collapsed;
                rbtnPOrderFDesc.Visibility = Visibility.Collapsed;
            }

        }

        private void txtSortByS_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (((int)txtSortSecond.Tag) == ((int)txtSortFirst.Tag))
            {
                txtSortSecond.Tag = 0;
                txtSortSecond.Text = "";
                lstSortSecond.SelectedIndex = -1;

                btnSortThird.IsEnabled = false;
                txtSortThird.IsTabStop = false;
                txtSortThird.Tag = 0;
                txtSortThird.Text = "";
                lstSortThird.SelectedIndex = -1;
            }
            else
            {
                if (((int)txtSortSecond.Tag) > 0)
                {
                    btnSortThird.IsEnabled = true;
                    txtSortThird.IsTabStop = true;
                    if (((int)txtSortSecond.Tag) == ((int)txtSortThird.Tag))
                    {
                        txtSortThird.Tag = 0;
                        txtSortThird.Text = "";
                        lstSortThird.SelectedIndex = -1;
                    }
                }
                else
                {
                    btnSortThird.IsEnabled = false;
                    txtSortThird.IsTabStop = false;
                    txtSortThird.Tag = 0;
                    txtSortThird.Text = "";
                    lstSortThird.SelectedIndex = -1;
                }
            }

            if ((int)txtSortSecond.Tag == 1)
            {
                rbtnPOrderSAsc.Visibility = Visibility.Visible;
                //default
                rbtnPOrderSAsc.IsChecked = true;
                rbtnPOrderSDesc.Visibility = Visibility.Visible;
            }
            else if (rbtnPOrderSAsc.Visibility == Visibility.Visible)
            {
                rbtnPOrderSAsc.Visibility = Visibility.Collapsed;
                rbtnPOrderSDesc.Visibility = Visibility.Collapsed;
            }

        }

        private void txtSortByT_TextChanged(object sender, TextChangedEventArgs e)
        {
            if ((((int)txtSortThird.Tag) == ((int)txtSortFirst.Tag)) || (((int)txtSortThird.Tag) == ((int)txtSortSecond.Tag)))
            {
                txtSortThird.Tag = 0;
                txtSortThird.Text = "";
                lstSortThird.SelectedIndex = -1;
            }

            if ((int)txtSortThird.Tag == 1)
            {
                rbtnPOrderTAsc.Visibility = Visibility.Visible;
                //default
                rbtnPOrderTAsc.IsChecked = true;
                rbtnPOrderTDesc.Visibility = Visibility.Visible;
            }
            else if (rbtnPOrderTAsc.Visibility == Visibility.Visible)
            {
                rbtnPOrderTAsc.Visibility = Visibility.Collapsed;
                rbtnPOrderTDesc.Visibility = Visibility.Collapsed;
            }
        }

        //btnSortBy
        private void btnSortBy_Click(object sender, RoutedEventArgs e)
        {
            ListBox lstAux = null;
            TextBox txtAux = null;
            Storyboard stbAux = null;

            switch (((Button)sender).Name)
            {
                case "btnSortFirst":
                    lstAux = lstSortFirst;
                    txtAux = txtSortFirst;
                    stbAux = toVisibleSortFirst;
                    break;
                case "btnSortSecond":
                    lstAux = lstSortSecond;
                    txtAux = txtSortSecond;
                    stbAux = toVisibleSortSecond;
                    break;
                case "btnSortThird":
                    lstAux = lstSortThird;
                    txtAux = txtSortThird;
                    stbAux = toVisibleSortThird;
                    break;
            }

            if (lstAux.Visibility == Visibility.Collapsed)
            {

                if (lstAux.SelectedItem == null)
                {
                    if (lstAux.Name.Equals("lstSortFirst"))
                    {
                        lstAux.SelectedIndex = 0;
                    }
                    else if (lstAux.Name.Equals("lstSortSecond"))
                    {
                        //Si el Sort First tiene seleccionado el primer elemento, 
                        //entonces que éste seleccione el seg, en los otros 2 casos posibles
                        //éste selecciona el primero (Acordarse: Tag = Index del ListBox + 1)
                        if (((int)txtSortFirst.Tag) == 1)
                        {
                            lstAux.SelectedIndex = 1;
                        }
                        else
                        {
                            lstAux.SelectedIndex = 0;
                        }
                    }
                    else
                    {
                        if (!((((int)txtSortFirst.Tag) == 1) || (((int)txtSortSecond.Tag) == 1)))
                        {
                            lstAux.SelectedIndex = 0;
                        }
                        else if (!((((int)txtSortFirst.Tag) == 2) || (((int)txtSortSecond.Tag) == 2)))
                        {
                            lstAux.SelectedIndex = 1;
                        }
                        else
                        {
                            lstAux.SelectedIndex = 2;
                        }
                    }

                }

                lstAux.Visibility = Visibility.Visible;
                stbAux.Begin();
            }
            else
            {
                txtAux.Focus();
            }
        }

        //Lst SortBy
        private void lstSortBy_LostFocus(object sender, RoutedEventArgs e)
        {
            if (!FocusManager.GetFocusedElement().GetType().ToString().Equals("System.Windows.Controls.ListBoxItem"))
            {
                ((ListBox)sender).Visibility = Visibility.Collapsed;
            }
            else
            {
                ListBoxItem item = ((ListBoxItem)FocusManager.GetFocusedElement());
                Boolean encontro = false;
                int indice = 0;
                while (!encontro && indice < ((ListBox)sender).Items.Count)
                {
                    if (((ListBox)sender).Items[indice].Equals(item))
                    {
                        encontro = true;
                    }
                    else
                    {
                        indice += 1;
                    }
                }

                if (!encontro)
                {
                    ((ListBox)sender).Visibility = Visibility.Collapsed;
                }

            }
        }
        private void lstSortBy_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (((ListBox)sender).SelectedItem != null && !((ListBox)sender).SelectedItem.Equals(sender))
            {
                TextBox txtAux = null;
                RadioButton rbtnAsc = null;
                RadioButton rbtnDesc = null;

                switch (((ListBox)sender).Name)
                {
                    case "lstSortFirst":
                        txtAux = txtSortFirst;
                        rbtnAsc = rbtnPOrderFAsc;
                        rbtnDesc = rbtnPOrderFDesc;
                        break;
                    case "lstSortSecond":
                        txtAux = txtSortSecond;
                        rbtnAsc = rbtnPOrderSAsc;
                        rbtnDesc = rbtnPOrderSDesc;
                        break;
                    case "lstSortThird":
                        txtAux = txtSortThird;
                        rbtnAsc = rbtnPOrderTAsc;
                        rbtnDesc = rbtnPOrderTDesc;
                        break;
                }

                txtAux.IsTabStop = false;
                txtAux.Text = ((TextBlock)((ListBoxItem)((ListBox)sender).SelectedItem).Content).Text;
                txtAux.Tag = ((ListBox)sender).SelectedIndex + 1;
                txtAux.IsTabStop = true;

                /*
                if ((int)txtAux.Tag==1)
                {
                    rbtnAsc.Visibility = Visibility.Visible;
                    rbtnDesc.Visibility = Visibility.Visible;
                }
                else if (rbtnAsc.Visibility == Visibility.Visible)
                {
                    rbtnAsc.Visibility = Visibility.Collapsed;
                    rbtnDesc.Visibility = Visibility.Collapsed;
                } 
                */
            }
        }
        private void toVisibleSortByF_Completed(object sender, EventArgs e)
        {
            if (lstSortFirst.SelectedIndex > -1)
            {
                ((ListBoxItem)lstSortFirst.Items[lstSortFirst.SelectedIndex]).Focus();
            }
        }

        private void toVisibleSortByS_Completed(object sender, EventArgs e)
        {
            if (lstSortSecond.SelectedIndex > -1)
            {
                ((ListBoxItem)lstSortSecond.Items[lstSortSecond.SelectedIndex]).Focus();
            }
        }

        private void toVisibleSortByT_Completed(object sender, EventArgs e)
        {
            if (lstSortThird.SelectedIndex > -1)
            {
                ((ListBoxItem)lstSortThird.Items[lstSortThird.SelectedIndex]).Focus();
            }
        }


        //Item SortBy
        private void itemSortByF_KeyDown(object sender, KeyEventArgs e)
        {
            txtSortFirst.Focus();
        }

        private void itemSortByS_KeyDown(object sender, KeyEventArgs e)
        {
            txtSortSecond.Focus();
        }

        private void itemSortByT_KeyDown(object sender, KeyEventArgs e)
        {
            txtSortThird.Focus();
        }

        private void itemSortByF_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            txtSortFirst.Focus();
        }

        private void itemSortByS_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            txtSortSecond.Focus();
        }

        private void itemSortByT_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            txtSortThird.Focus();
        }


        //RadioButtons        
        private void rbtnPOrderAsc_Checked(object sender, RoutedEventArgs e)
        {
            rbtnPOrderAsc.IsChecked = ((RadioButton)sender).IsChecked;
        }

        private void rbtnPOrderDesc_Checked(object sender, RoutedEventArgs e)
        {
            rbtnPOrderDesc.IsChecked = ((RadioButton)sender).IsChecked;

        }

        public void ChangeTextComboBox(int language)
        {
            TextLanguage text = new TextLanguage();

            //String aux = ((string)txtFilCategory.Tag); 

            //ComboBox Category
            /*
            if (((string)txtFilCategory.Tag).Equals("17"))            
            {
                txtFilCategory.Text = text.GetText(TextLanguage.fields.adv_filt_cat_LF, language);
            }
            else if (((Int32)txtFilCategory.Tag)==13996)
            {
                txtFilCategory.Text = text.GetText(TextLanguage.fields.adv_filt_cat_medicine, language);
            }
            else if (((Int32)txtFilCategory.Tag)==18)
            {
                txtFilCategory.Text = text.GetText(TextLanguage.fields.adv_filt_cat_MT, language);    
            }
            else if (((Int32)txtFilCategory.Tag) == 53)
            {
                txtFilCategory.Text = text.GetText(TextLanguage.fields.adv_filt_cat_nonfiction, language);
            }
            else if (((Int32)txtFilCategory.Tag)==290060)
            {
                txtFilCategory.Text = text.GetText(TextLanguage.fields.adv_filt_cat_ON, language);
            }
            */
        
            switch ((int)txtSortFirst.Tag)
            {
                case 1:
                    txtSortFirst.Text = text.GetText(TextLanguage.fields.adv_sort_fLPrice, language);
                    break;
                case 2:
                    txtSortFirst.Text = text.GetText(TextLanguage.fields.adv_sort_fPublication, language);
                    break;
                case 3:
                    txtSortFirst.Text = text.GetText(TextLanguage.fields.adv_sort_fTitle, language);
                    break;
            }

            switch ((int)txtSortSecond.Tag)
            {
                case 1:
                    txtSortSecond.Text = text.GetText(TextLanguage.fields.adv_sort_fLPrice, language);
                    break;
                case 2:
                    txtSortSecond.Text = text.GetText(TextLanguage.fields.adv_sort_fPublication, language);
                    break;
                case 3:
                    txtSortSecond.Text = text.GetText(TextLanguage.fields.adv_sort_fTitle, language);
                    break;
            }

            switch ((int)txtSortThird.Tag)
            {
                case 1:
                    txtSortThird.Text = text.GetText(TextLanguage.fields.adv_sort_fLPrice, language);
                    break;
                case 2:
                    txtSortThird.Text = text.GetText(TextLanguage.fields.adv_sort_fPublication, language);
                    break;
                case 3:
                    txtSortThird.Text = text.GetText(TextLanguage.fields.adv_sort_fTitle, language);
                    break;
            }                        
        }

        #endregion

        #region Clean Filds

        private void CleanFields()
        {
            txtFilAuthor.Text = "";
            txtFilPublisher.Text = "";
            txtFilTitle.Text = "";
            txtFilKeyword.Text = "";
            rbtnCndUsed.IsChecked = false;
            rbtnCndCollectible.IsChecked = false;
            rbtnCndRefurbished.IsChecked = false;
            txtFilCategory.Tag = 0;
            txtFilCategory.Text = "";
            txtFilMaxPrice.Text = "";
            txtFilMinPrice.Text = "";

            txtSortFirst.Tag = 0;
            txtSortFirst.Text = "";
            txtSortSecond.Tag = 0;
            txtSortSecond.Text = "";
            txtSortThird.Tag = 0;
            txtSortThird.Text = "";

            rbtnPOrderFAsc.IsChecked = false;
            rbtnPOrderFDesc.IsChecked = false;
        }

        private void hlkClean_Click(object sender, RoutedEventArgs e)
        {
            CleanFields();
            txtFilAuthor.Focus();
        }

        #endregion

        #region SetFocus
        
        private void txtFilAuthor_KeyDown(object sender, KeyEventArgs e)
        {
            if (System.Windows.Input.Key.Enter == e.Key)
            {
                txtFilPublisher.Focus();
            }
        }

        private void txtFilPublisher_KeyDown(object sender, KeyEventArgs e)
        {
            if (System.Windows.Input.Key.Enter == e.Key)
            {
                txtFilTitle.Focus();
            }
        }

        private void txtFilTitle_KeyDown(object sender, KeyEventArgs e)
        {
            if (System.Windows.Input.Key.Enter == e.Key)
            {
                txtFilKeyword.Focus();
            }
        }

        private void txtFilKeyword_KeyDown(object sender, KeyEventArgs e)
        {
            if (System.Windows.Input.Key.Enter == e.Key)
            {
                rbtnCndAll.Focus();
            }
        }

        private void rbtnCnd_KeyDown(object sender, KeyEventArgs e)
        {
            if (System.Windows.Input.Key.Enter == e.Key)
            {
                txtFilCategory.Focus();
            }            
        }

        private void txtFilMaxPrice_KeyDown(object sender, KeyEventArgs e)
        {
            if (System.Windows.Input.Key.Enter == e.Key)
            {
                txtFilMinPrice.Focus();
            }             
                 
        }

        private void txtFilMinPrice_KeyDown(object sender, KeyEventArgs e)
        {
            if (System.Windows.Input.Key.Enter == e.Key)
            {
                txtSortFirst.Focus();
            }
        }

        #endregion

    }
}
